#!/usr/bin/env bash
#
# platform
#
MEM=$(free -m | grep Mem | awk '{print $2}')
HALF_MEM=$((MEM / 2))
MIN_MEM=$((HALF_MEM / 3))
JAVA_OPTS="-server -Xmx${HALF_MEM}m -Xms${HALF_MEM}m -Xmn${MIN_MEM}m -Xss1m"

DIR="$(cd "$(dirname "$0")" && pwd)"
APP_FILE="${DIR}/platform-boot-1.0.0-exec.jar"
APP_ARGS="--spring.profiles.active=sit"

export LD_LIBRARY_PATH=${DIR}:$LD_LIBRARY_PATH

main() {
  PID=$(pgrep -fn "${APP_FILE}")
  if [ -z "${PID}" ]; then
    echo "Starting platform: java ${JAVA_OPTS} -jar ${APP_FILE} ${APP_ARGS}"
    nohup java $JAVA_OPTS -XX:MaxMetaspaceSize=256m -XX:InitialCodeCacheSize=240m -XX:ReservedCodeCacheSize=240m -XX:+AlwaysPreTouch -XX:ParallelGCThreads=8 -XX:+UseG1GC -Dfile.encoding=UTF-8 -Djava.awt.headless=true -Duser.timezone=Asia/Shanghai -jar $APP_FILE $APP_ARGS >/dev/null 2>&1 &
    echo ".................. [OK]"
  else
    echo "platform is running... pid: ${PID}"
  fi
  return 0
}

main "$@"
exit $?
